﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace ExpandPanelControl
{
    public partial class ExpandPanel : ContainerControl           
    {
        //定义面板展开状态改变事件委托
        public delegate void ExpandStatusChangedEventHandler(object sender, EventArgs e);
        //定义面板展开状态改变事件
        public event ExpandStatusChangedEventHandler ExpandStatusChangedEvent;

        /// <summary>
        /// 面板图标
        /// </summary>
        private Image _PanelIcon=Properties .Resources .PanelIcon ;
        [Browsable (true ),Category ("面板图标"),Description ("获取或设置面板图标")]
        public Image PanelIcon
        {
            get
            {
                return _PanelIcon;
            }
            set
            {
                _PanelIcon = value;
                pPanelIcon.Image = _PanelIcon;
            }
        }

        /// <summary>
        /// 面板展开时的高度
        /// </summary>
        private int _PanelHeight_Expanded;
        [Browsable(true), Category("面板展开时的高度"), Description("获取或设置面板展开时的高度")]
        public int PanelHeight_Expanded
        {
            get
            {
                return _PanelHeight_Expanded;
            }
            set
            {
                _PanelHeight_Expanded = value;
                if (Expanded)
                {
                    this.Height = _PanelHeight_Expanded;
                }
            }
        }

        /// <summary>
        /// 面板收起时的高度
        /// </summary>
        private int _PanelHeight_Unexpanded=30;
        [Browsable(true), Category("面板收起时的高度"), Description("获取面板收起时的高度，固定为30")]
        public int PanelHeight_Unexpanded
        {
            get
            {
                return _PanelHeight_Unexpanded;
            }
        }

        /// <summary>
        /// 面板折叠状态
        /// </summary>
        private bool _Expanded=true ;
        [Browsable(true), Category("面板折叠状态"), Description("获取或设置面板折叠状态")]
        public bool Expanded
        {
            get
            {
                return _Expanded;
            }
            set
            {
                _Expanded = value;
                ChangePanelExpandStatus();
            }
        }

        /// <summary>
        /// 面板标题
        /// </summary>
        private string _Text = "ExpandPanel";
        [Browsable(true), Category("面板标题"), Description("获取或设置面板标题")]
        public override string Text
        {
            get
            {
                return _Text;
            }
            set
            {
                _Text = value;
                lTitle.Text = _Text;
            }
        }

        public ExpandPanel()
        {
            InitializeComponent();
        }

        private void ExpandPanel_Paint(object sender, PaintEventArgs e)
        {
            //Graphics g = e.Graphics;
            //g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            //g.DrawRectangle(new Pen(Color.Black, 1), 1, 1, this.Width-2, this.Height-2);
            //g.DrawImage((Image)Properties.Resources.PanelIcon, 2, 2, 23, 23);
        }

        private void DrawBorder()
        {
            Graphics g = this.CreateGraphics();
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            g.DrawRectangle(new Pen(Color.Black, 1), 1, 1, this.Width - 2, this.Height - 2);
        }

        private void pUpDownIcon_Click(object sender, EventArgs e)
        {
            Expanded = !Expanded;
        }

        private void ChangePanelExpandStatus()
        {
            if (Expanded)
            {
                this.pUpDownIcon.Image = Properties .Resources .PanelUp ;
                this.Height = PanelHeight_Expanded;
            }
            else
            {
                this.pUpDownIcon.Image = Properties .Resources .PanelDown ;
                this.Height = PanelHeight_Unexpanded;
            }
            Invalidate();
            if (ExpandStatusChangedEvent != null)
            {
                ExpandStatusChangedEvent(this, new EventArgs());
            }
        }

        private void ExpandPanel_Resize(object sender, EventArgs e)
        {
            if (Expanded)
            {
                pTitle.Width = this.Width - 2;
                PanelHeight_Expanded = this.Height;
            }
            Invalidate();
            DrawBorder();
        }
    }
}
